package com.it.myh;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: Od_0221_05    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2023/2/21 16:19   // 时间
 * @Version: 1.0     // 版本
 */
public class Od_0221_05 {

    //服务端
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        try {
            //1.创建服务端的socket：ServerSocket，指明自己的端口号
            serverSocket = new ServerSocket(8899);
            //2.通过ServerSocket对象来接收客户端的socket
            accept = serverSocket.accept();
            //3.获取输入流
            inputStream = accept.getInputStream();
            //4.读取输入流当中的数据
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1){
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关流
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept != null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
